package com.xike.microemailserver.service.impl;

import com.alibaba.fastjson.JSON;
import com.xike.entiy.ResultBean;
import com.xike.microemailserver.bean.MsgParam;
import com.xike.microemailserver.service.IMessageService;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author: masterhai
 * @date: 2021/4/6:9:35
 * @description:
 */
@Service
public class MessageServiceImpl  implements IMessageService {
    @Resource
    private ValueOperations valueOperations;

    @Override
    public ResultBean sendMessage (String phoneNum,String msg) {
        System.out.println(phoneNum+msg);
        //创建5位数随机验证码
        String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 5);
        //将验证码存入redis,5分钟有效,如果存在则不发送新验证码
        if (! valueOperations.setIfAbsent(phoneNum + ":msg", msg, 1, TimeUnit.DAYS)) {
            return ResultBean.builder().code(601).msg("24小时内已发送过短信").build();
        }
        // 获得Http客户端
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        URI uri = null;
        try {
            // 设置uri信息
            uri = new
                    URIBuilder().setScheme("http").setHost("pub.woniulab.com").setPort(8081)
                    .setPath("/msg/sendMsg").build();
        } catch (URISyntaxException e1) {
            e1.printStackTrace();
        }
        MsgParam param = new MsgParam();
        param.setMsg(msg);
        param.setCode("");//支持[0-9][a-z][A-Z]
        param.setPhoneNum(phoneNum);//发送手机号码
        // 创建Post请求
        HttpPost httpPost = new HttpPost(uri);
        StringEntity entity = new StringEntity(JSON.toJSONString(param),
                "UTF-8");
        // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
        httpPost.setEntity(entity);
        httpPost.setHeader("Content-Type", "application/json;charset=utf8");
        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Post请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return ResultBean.builder().code(200).msg("短信发送成功").data(msg).build();
    }

//    @Override
//    public void sendMail () {
//        SimpleMailMessage message = new SimpleMailMessage();
//
//        MailBean mailInfo = mailDao.getMailInfo();
//        //查询数据库获取目的邮箱
//        message.setTo(mailInfo.getSendUser());
//        message.setSubject("异常提醒邮件");
//        message.setText("测试邮件发送");
//        message.setFrom(mailInfo.getSendUser());
//        System.out.println("开始发邮件,对象:" + myMailSender);
//       // System.out.println("":" + myMailSender);
//        myMailSender.send(message);
//    }

//    @Override
//    protected PasswordAuthentication getPasswordAuthentication() {
//        MailBean mailInfo = mailDao.getMailInfo();
//        return new PasswordAuthentication(mailInfo.get, pwd);
//    }
}
